google.load('search', '1');

var imageSearch;

function addPaginationLinks() {

    // To paginate search results, use the cursor function.
    var cursor = imageSearch.cursor;
    var curPage = cursor.currentPageIndex; // check what page the app is on
    var pagesDiv = document.createElement('div');
    for (var i = 0; i < cursor.pages.length; i++) {
        var page = cursor.pages[i];
        if (curPage == i) { 

            // If we are on the current page, then don't make a link.
            var label = document.createTextNode(' ' + page.label + ' ');
            pagesDiv.appendChild(label);
        } else {

            // Create links to other pages using gotoPage() on the searcher.
            var link = document.createElement('a');
            link.href = 'javascript:imageSearch.gotoPage('+i+');';
            link.innerHTML = page.label;
            link.style.marginRight = '2px';
            pagesDiv.appendChild(link);
        }
    }

    var contentDiv = document.getElementById('content');
    contentDiv.appendChild(pagesDiv);
}

function searchComplete() {

    // Check that we got results
    if (imageSearch.results && imageSearch.results.length > 0) {

        // Grab our content div, clear it.
        var contentDiv = document.getElementById('content');
        contentDiv.innerHTML = '';

        // Loop through our results, printing them to the page.
        var results = imageSearch.results;
        for (var i = 0; i < results.length; i++) {
            // For each result write it's title and image to the screen
            var result = results[i];
            var imgContainer = document.createElement('a');
            imgContainer.href = result.unescapedUrl;
            imgContainer.className = 'imagenes img'+(i+1);
            imgContainer.id = 'foto'+(i+1);

            var newImg = document.createElement('img');

            // There is also a result.url property which has the escaped version
            newImg.src = result.tbUrl;

            imgContainer.appendChild(newImg);

            // Put our title + image in the content
            contentDiv.appendChild(imgContainer);
            
            // Colocamos el link en el formuario
            imgContainer.onclick= function(){
              
              // Establecemos dentro del formulario la dirección url
              document.getElementById('medio_imagen_codigo').value= this.href;
              
              // Resaltamos la imagen, y desvanecemos las demás.
              $("a.imagenes").removeClass('imagen_normal');
              $("a.imagenes").addClass('imagen_opaca');
              $(this).removeClass('imagen_opaca');
              $(this).addClass('imagen_normal');
              
            }
        }
    }
}
  
function determinarSize(){
    var numero = Math.floor(Math.random()*11);
    if(numero < 4){
        return ' small';
    }
    else if(numero < 6){
        return ' medium';
    }
    else{
        return ' large';
    }
}
  

function OnLoad(busqueda) {

    // Create an Image Search instance.
    imageSearch = new google.search.ImageSearch();

    imageSearch.setResultSetSize(8);

    // Set searchComplete as the callback function when a search is 
    // complete.  The imageSearch object will have results in it.
    imageSearch.setSearchCompleteCallback(this, searchComplete, null);

    // Realiza la búsqueda
    imageSearch.execute(busqueda);

    // Include the required Google branding
    google.search.Search.getBranding('branding');
}



function SearchGoogleImages(busqueda){
    
    // Limpiamos la busqueda pasada
    imageSearch.clearResults();
    
    //Cargando
    var contentDiv = document.getElementById('content');
    contentDiv.innerHTML = '<img id="loader_google" src="../../../images/loader.gif" alt ="loading"/>';
    
    // Realiza la búsqueda
    imageSearch.execute(busqueda); 
    
    
    
}
